// 封装购物车模块

import { defineStore } from 'pinia'
import { computed, ref,watch } from 'vue'
import { addCartAPI,getCartListAPI,delCartAPI } from '@/apis/cart.js'
import { useUserStore } from '@/stores/userStore'

export const useCartStore = defineStore('cart', () => {
  const userStore = useUserStore()
  const Login=computed(()=>userStore.userInfo.data)
  // 1. 定义state - cartList
  const cartList = ref([])
  // 2. 定义action - addCart

 //获取最新购物车列表
 const getNewCartList = async() => {
  const result=await getCartListAPI()
  cartList.value=result.data.data
}

  const addCart = async(goods) => {
    const {id,count}=goods
    if(true){
      console.log('token222',Login.value);
      
      //登录后添加购物车
     const res= await addCartAPI({id,count})
     console.log('登录后添加',res.value);
     getNewCartList()
    }else{
        const item = cartList.value.find((item) => goods.id === item.id)
      if (item) {
        // 找到了
        item.count++
      } else {
        // 没找到
        cartList.value.push(goods)
      }
    }
}

  //删除购物车
  const delCart = async(id) => {
    console.log("iddddddddddddddddddddd",id)
    if(true){
      //登录后删除
      await delCartAPI(id)
      getNewCartList()
    }else{
      const index=cartList.value.filter((item) => item.id === id)
      cartList.value.splice(index,1)
    }
    
  }

  //清除购物车
  const clearCart = () => {
    cartList.value=[]
  }
const addCount=()=>{
  let result=0
  cartList.value.forEach((item)=>{
    result+=item.count
  })
  return result
}
const addPrice=()=>{
  let result=0
  cartList.value.forEach((item)=>{
    result+=item.price*item.count
  })
  return result
}
const selectedCountfunc=()=>{
  let result=0
  cartList.value.forEach((item)=>{
    result+=item.selected?item.count:0
  })
  return result
}
const selectedPricefunc=()=>{
  let result=0
  cartList.value.forEach((item)=>{
    result+=item.selected?item.price*item.count:0
  })
  return result
}
 

  //计算属性
  //总的数量
  const allCount=ref()
  //总价格
  const allPrice=ref()
  watch(cartList,(oldValue,newValue)=>{
    allCount.value=addCount()
    allPrice.value=addPrice()
  },{
    "deep":true
  })
  const selectedCount=ref()
  const selectedPrice=ref()
  watch(cartList,(oldValue,newValue)=>{
    selectedCount.value=selectedCountfunc()
    selectedPrice.value=selectedPricefunc()
  },
  {
    "deep":true
  }
  )

  //单选
  const singleCheck=(id,selected)=>{
   const item= cartList.value.find((item)=>item.id===id)
   item.selected=selected
  }

  //全选
  const isAll=computed(()=>cartList.value.every((item)=>item.selected))
  const allCheck=(selected)=>{
   cartList.value.forEach((item)=>item.selected=selected)
  }

  //需要购买的商品
  const SelectList=ref([])
  const SelectCount=ref(0)
  const SelectPrice=ref(0)
  
  const getSelectList=()=>{
    SelectList.value=[]
    SelectCount.value=0
    SelectPrice.value=0
    cartList.value.forEach((item)=>{
      if(item.selected){
        SelectList.value.push(item)
        console.log("选中的结果",item)
        SelectCount.value+=item.count
        SelectPrice.value+=item.price*item.count
      }
    })
  }

  const changeCartListCount=(id,count)=>{
    cartList.value.forEach((item)=>{
      if(item.id==id){
        item.count=count
      }
    })
  }
  // getSelectList()
 

  //订单单号
  const orderId=ref()
  const getOrderId=(data)=>{
    orderId.value=data
  }
  


  return {
    cartList,
    addCart,
    delCart,
    allCount,
    allPrice,
    singleCheck,
    isAll,
    allCheck,
    selectedCount,
    selectedPrice,
    clearCart,
    getNewCartList,
    SelectList,
    getSelectList,
    changeCartListCount,
    SelectCount,
    SelectPrice,
    orderId,
    getOrderId
  }
}, {
  persist: true,
})